﻿// *******************************************************************************************************
// Example code to show how to read and write to a 24C32 EEPROM using the Devantech USB-I2C Module.
// Writen in Visual C# Express 2008.
// Before using this code you must have installed the FTDI VCP drivers. 
// The VCP drivers make the USB-I2C appear as a serial Port on your PC.
// See http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm
// NOTE!   This code contains no error handling. 
//         It's point is to show how to use C#, the USB-I2C and an EEPROM.
// *******************************************************************************************************
// The 24C32 uses a 2-byte internal register address
// *******************************************************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Resources;
using Microsoft.Win32;

namespace EE24C32
{
     public partial class EE24C32 : Form
    {
       static SerialPort USB_I2C;
       private double oldTemp = 0;
       private byte[] T_R_Addr = new byte[8];
       private byte[] T_W_Addr = new byte[8];
       private bool[] T_Present = new bool[8];
    //   private bool firstRead = true;
        public EE24C32()
        {
           InitializeComponent();

           RegistryKey RegKey = Registry.LocalMachine;
           RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
           textCPUVendor.Text = string.Format("{0}", RegKey.GetValue("VendorIdentifier"));
           textCPUSpeed.Text = string.Format("{0}", RegKey.GetValue("~MHz"));
           buttonSensorsDetectStop.Enabled = false;
           buttonSensorsDetect.Enabled = false;

              T_R_Addr[0] = 0x91;
              T_R_Addr[1] = 0x93;
              T_R_Addr[2] = 0x95;
              T_R_Addr[3] = 0x97;
              T_R_Addr[4] = 0x99;
              T_R_Addr[5] = 0x9B;
              T_R_Addr[6] = 0x9D;
              T_R_Addr[7] = 0x9F;

              T_W_Addr[0] = 0x90;
              T_W_Addr[1] = 0x92;
              T_W_Addr[2] = 0x94;
              T_W_Addr[3] = 0x96;
              T_W_Addr[4] = 0x98;
              T_W_Addr[5] = 0x9A;
              T_W_Addr[6] = 0x9C;
              T_W_Addr[7] = 0x9E;

              T_Present[0] = false;
              T_Present[1] = false;
              T_Present[2] = false;
              T_Present[3] = false;
              T_Present[4] = false;
              T_Present[5] = false;
              T_Present[6] = false;
              T_Present[7] = false;

              for (int i = 0; i <= 7; i++)
              {
                  checkConfigFieldsAvailable(i);
              }

            USB_I2C = new SerialPort();
            // Get a list of serial port names.
            foreach (string s in SerialPort.GetPortNames())
            {
                Ports.Items.Add(s);
            }
            refreshUpdateDisplay();

        }
     
        private void checkConfigFieldsAvailable(int sensId)
        {
            switch (sensId)
            {
                case 0:
                    if (T_Present[0])
                    {
                        customeDescription90.Enabled = true;
                        checkBoxActive90.Enabled = true;
                        radioDefault90.Enabled = true;
                    }
                    else
                    {
                        customeDescription90.Enabled = false;
                        checkBoxActive90.Enabled = false;
                        radioDefault90.Enabled = false;
                    }
                    break;
                case 1:
                    if (T_Present[1])
                    {
                        customeDescription92.Enabled = true;
                        checkBoxActive92.Enabled = true;
                        radioDefault92.Enabled = true;
                    }
                    else
                    {
                        customeDescription92.Enabled = false;
                        checkBoxActive92.Enabled = false;
                        radioDefault92.Enabled = false;
                    }
                    break;
                case 2:
                    if (T_Present[2])
                    {
                        customeDescription94.Enabled = true;
                        checkBoxActive94.Enabled = true;
                        radioDefault94.Enabled = true;
                    }
                    else
                    {
                        customeDescription94.Enabled = false;
                        checkBoxActive94.Enabled = false;
                        radioDefault94.Enabled = false;
                    }
                    break;
                case 3:
                    if (T_Present[3])
                    {
                        customeDescription96.Enabled = true;
                        checkBoxActive96.Enabled = true;
                        radioDefault96.Enabled = true;
                    }
                    else
                    {
                        customeDescription96.Enabled = false;
                        checkBoxActive96.Enabled = false;
                        radioDefault96.Enabled = false;
                    }
                    break;
                case 4:
                    if (T_Present[4])
                    {
                        customeDescription98.Enabled = true;
                        checkBoxActive98.Enabled = true;
                        radioDefault98.Enabled = true;
                    }
                    else
                    {
                        customeDescription98.Enabled = false;
                        checkBoxActive98.Enabled = false;
                        radioDefault98.Enabled = false;
                    }
                    break;
                case 5:
                    if (T_Present[5])
                    {
                        customeDescription9A.Enabled = true;
                        checkBoxActive9A.Enabled = true;
                        radioDefault9A.Enabled = true;
                    }
                    else
                    {
                        customeDescription9A.Enabled = false;
                        checkBoxActive9A.Enabled = false;
                        radioDefault9A.Enabled = false;
                    }
                    break;
                case 6:
                    if (T_Present[6])
                    {
                        customeDescription9C.Enabled = true;
                        checkBoxActive9C.Enabled = true;
                        radioDefault9C.Enabled = true;
                    }
                    else
                    {
                        customeDescription9C.Enabled = false;
                        checkBoxActive9C.Enabled = false;
                        radioDefault9C.Enabled = false;
                    }
                    break;
                case 7:
                    if (T_Present[7])
                    {
                        customeDescription9E.Enabled = true;
                        checkBoxActive9E.Enabled = true;
                        radioDefault9E.Enabled = true;
                    }
                    else
                    {
                        customeDescription9E.Enabled = false;
                        checkBoxActive9E.Enabled = false;
                        radioDefault9E.Enabled = false;
                    }
                    break;
            }
        }

        private void sensorsInitiation()
        {
            for (int i = 0; i <= 7; i++)
            {
                if (T_Present[i])
                {
                    dsPowerBackConfig(T_W_Addr[i], 0x02);
                    dsCommStartConvert(T_W_Addr[i]); 
                }
            }
        }

        private void refreshUpdateDisplay()
        {
            if (T_Present[0])
            {
                checkBoxActiveFront90.Text = customeDescription90.Text;
            }
            else
            {
                checkBoxActiveFront90.Visible = false;
                digitalDisplayControl90.DigitColor = System.Drawing.Color.DarkSlateGray;
            }

            if (T_Present[1])
            {
                checkBoxActiveFront92.Text = customeDescription92.Text;
            }
            else
            {
                checkBoxActiveFront92.Visible = false;
                digitalDisplayControl92.DigitColor = System.Drawing.Color.DarkSlateGray;
            }
            if (T_Present[2])
            {
                checkBoxActiveFront94.Text = customeDescription94.Text;
            }
            else
            {
                checkBoxActiveFront94.Visible = false;
                digitalDisplayControl94.DigitColor = System.Drawing.Color.DarkSlateGray;
            }
            if (T_Present[3])
            {
                checkBoxActiveFront96.Text = customeDescription96.Text;
            }
            else
            {
                checkBoxActiveFront96.Visible = false;
                digitalDisplayControl96.DigitColor = System.Drawing.Color.DarkSlateGray;
            }
            if (T_Present[4])
            {
                checkBoxActiveFront98.Text = customeDescription98.Text;
            }
            else
            {
                checkBoxActiveFront98.Visible = false;
                digitalDisplayControl98.DigitColor = System.Drawing.Color.DarkSlateGray;
            }
            if (T_Present[5])
            {
                checkBoxActiveFront9A.Text = customeDescription9A.Text;
            }
            else
            {
                checkBoxActiveFront9A.Visible = false;
                digitalDisplayControl9A.DigitColor = System.Drawing.Color.DarkSlateGray;
            }
            if (T_Present[6])
            {
                checkBoxActiveFront9C.Text = customeDescription9C.Text;
            }
            else
            {
                checkBoxActiveFront9C.Visible = false;
                digitalDisplayControl9C.DigitColor = System.Drawing.Color.DarkSlateGray;
            }
            if (T_Present[7])
            {
                checkBoxActiveFront9E.Text = customeDescription9E.Text;
            }
            else
            {
                checkBoxActiveFront9E.Visible = false;
                digitalDisplayControl9E.DigitColor = System.Drawing.Color.DarkSlateGray;
            }
        }
     
        private void analogValueDraw(double myTemp)
        {
            double tempTemp = 0;
            double diferentTemp = 0;
            if (oldTemp != myTemp)
            {
                if (oldTemp < myTemp)
                {
                    tempTemp = oldTemp;
                    while (float.Parse(tempTemp.ToString()) != float.Parse(myTemp.ToString()))
                    {
                        diferentTemp = (myTemp - tempTemp) / 10;
                        if (diferentTemp < 0.0015)
                        {
                            tempTemp = myTemp;
                        }
                        else 
                        {
                            tempTemp = tempTemp + diferentTemp;
                        }                     
                        analogMeter1.Value = float.Parse(tempTemp.ToString());
                        analogMeter1.Text = "Temperature (oC)";
                        Thread.Sleep(6);
                    }
                    oldTemp = myTemp;
                }
                if (oldTemp > myTemp)
                {
                    tempTemp = oldTemp;
                    while (float.Parse(tempTemp.ToString()) != float.Parse(myTemp.ToString()))
                    {
                        diferentTemp = (tempTemp - myTemp) / 10;
                        if (diferentTemp < 0.0015)
                        {
                            tempTemp = myTemp;
                        }
                        else
                        {
                            tempTemp = tempTemp - diferentTemp;
                        } 
                        analogMeter1.Value = float.Parse(tempTemp.ToString());
                       // analogMeter1.Text = "Temperature (oC) going DOWN";
                        Thread.Sleep(6);
                    }
                    oldTemp = myTemp;
                }
            }

        }

        private void dsPowerBackConfig(byte address, byte configByte)
        {
            byte[] SerBuf = new byte[10];
            SerBuf[0] = 0x55;                   // Command for Devices with 2 address bytes 
            SerBuf[1] = address;                //0x90;// I2C address write
            SerBuf[2] = 0xac;                   // Access Config
            SerBuf[3] = 0x01;                   // number of bytes to write
            SerBuf[4] = configByte;             //0x03;// MSb Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 LSb
                                                //DONE THF  TLF  NVB  X    X    POL  1SHOT   
                                                // 0x03 4b
                                                // 0   1    0    0    1    0    1    1
                                                // 0x01 49
                                                // 0   1    0    0    1    0    0    1
            USB_I2C.Write(SerBuf, 0, 5);
            while (USB_I2C.BytesToRead < 1);    // wait for data to arrive from the chip
            USB_I2C.Read(SerBuf, 0, 1);
        }

        private void dsCommStartConvert(byte address)
        {
            byte[] SerBuf = new byte[10];
            SerBuf[0] = 0x53;                    // Command
            SerBuf[1] = address;                    // I2C address 
            SerBuf[2] = 0xee;                    // Start Convert T
            USB_I2C.Write(SerBuf, 0, 3);
            while (USB_I2C.BytesToRead < 1);     // wait for data to arrive from the chip
            USB_I2C.Read(SerBuf, 0, 1);
           // System.Threading.Thread.Sleep(900);
        }

        private void dsCheckBusy(byte address)
        {
            byte[] SerBuf = new byte[10];
            byte[] SerBufR = new byte[10];
            int busyCharValue = 0;
            do
            {
                SerBuf[0] = 0x55;                    // Command for Devices
                SerBuf[1] = 0x91;                    // I2C address for read
                SerBuf[2] = 0xac;                    // read config register
                SerBuf[3] = 0x01;                    // one byte
                USB_I2C.Write(SerBuf, 0, 4);
                while (USB_I2C.BytesToRead < 1) ;
                USB_I2C.Read(SerBufR, 0, 1);         // get the 8 data bytes
                //  busyChar = string.Format("{0:d03}", SerBufx[0]);
                busyCharValue = int.Parse(string.Format("{0:d03}", SerBufR[0]));
            } while (busyCharValue > 128);
        }

        private double dsCommReadTemperature(byte address, bool precise)
        {
            byte[] SerBuf = new byte[10];
            byte[] SerBufR = new byte[10];
            byte[] SerBufp1 = new byte[10];
            byte[] SerBufrp1 = new byte[10];
            byte[] SerBufp2 = new byte[10];
            byte[] SerBufrp2 = new byte[10];
            int myVartest;
            double counterPrecise;
            double slopePrecise;
            double myTempInprecise = 0;
            double myRealTemperature = 0;

            SerBuf[0] = 0x55;                    // Command for Devices
            SerBuf[1] = address;                    // I2C address for read
            SerBuf[2] = 0xaa;                    // read temperature
            SerBuf[3] = 0x02;                    // two bytes
            USB_I2C.Write(SerBuf, 0, 4);
            while (USB_I2C.BytesToRead < 2) ;
            USB_I2C.Read(SerBufR, 0, 2);          // get the 8 data bytes

            if (precise == false)
            {
                if (SerBufR[1] == 0)
                {
                    myVartest = 0;
                }
                else
                {
                    myVartest = 5;
                }
                return double.Parse(string.Format("{0:d02}.{1:d01}", SerBufR[0], myVartest));
                //myTempChar = string.Format("{0:d02}.{1:d01}", SerBufR[0], myVartest);
                //myTemp = float.Parse(myTempChar);
                //Temperature.Text = string.Format("{0:d02}.{1:d01}", SerBufR[0], myVartest);
            }
            else if (precise == true)
            {
                SerBufp1[0] = 0x55;                    // Command for Devices
                SerBufp1[1] = 0x91;                    // I2C address for read
                SerBufp1[2] = 0xa8;                    // read temperature
                SerBufp1[3] = 0x01;                    // one byte
                USB_I2C.Write(SerBufp1, 0, 4);
                while (USB_I2C.BytesToRead < 1) ;
                USB_I2C.Read(SerBufrp1, 0, 1);         // get the 8 data bytes
                counterPrecise = double.Parse(string.Format("{0:d03}", SerBufrp1[0]));

                SerBufp2[0] = 0x55;                    // Command for Devices
                SerBufp2[1] = 0x91;                    // I2C address for read
                SerBufp2[2] = 0xa9;                    // read temperature
                SerBufp2[3] = 0x01;                    // one byte
                USB_I2C.Write(SerBufp2, 0, 4);
                while (USB_I2C.BytesToRead < 1) ;
                USB_I2C.Read(SerBufrp2, 0, 1);          // get the 8 data bytes
                slopePrecise = double.Parse(string.Format("{0:d03}", SerBufrp2[0]));
                myTempInprecise = double.Parse(string.Format("{0:d03}", SerBufR[0]));
                myRealTemperature = myTempInprecise - 0.25 + ((slopePrecise - counterPrecise) / slopePrecise);
                // myTempChar = string.Format("{0:d02}.{1:d01}", SerBuf[0], myVartest);
                // myTemp = float.Parse(myTempChar);
                //myTempChar = Temperature.Text = String.Format("{0:0.0}", myRealTemperature);
                //myTemp = double.Parse(myTempChar);
                return double.Parse(String.Format("{0:0.0}", myRealTemperature));
            }
            else
            {
                return 0.0;
            }
        }

        private void Ports_SelectedIndexChanged(object sender, EventArgs e)
        {
            byte[] SerBuf = new byte[10];
            
            try
            {
                USB_I2C.Close();
                USB_I2C_VER.Text = "";
                USB_I2C.PortName = Ports.Text;
                USB_I2C.Parity = 0;
                USB_I2C.BaudRate = 19200;
                USB_I2C.StopBits = StopBits.Two;
                USB_I2C.DataBits = 8;
                USB_I2C.ReadTimeout = 500;
                USB_I2C.WriteTimeout = 500;
                USB_I2C.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please make sure that device is connected to this port.\nError opening/writing to serial port. "+ex.Message);
                USB_I2C.Close();
                return;
            }
            
            try
            {
                SerBuf[0] = 0x5A;    // USB_I2C Module Command
                SerBuf[1] = 0x01;     // Get Revision
                SerBuf[2] = 0;
                SerBuf[3] = 0;
                USB_I2C.Write(SerBuf, 0, 4);
                USB_I2C.Read(SerBuf, 0, 1);
                USB_I2C_VER.Text = string.Format("{0}", SerBuf[0]);
            }
            catch (TimeoutException ex)
            {
                MessageBox.Show("Please make sure that your device is conected properly.\nCan not read USB-I2C device\n" + ex.Message);
                return;
            }
            checkBoxActiveView90.Text = customeDescription90.Text; 
            buttonSensorsDetect.Enabled = true;
            for (int i = 0; i <= 7; i++)
            {
                checkConfigFieldsAvailable(i);
            }
            sensorsInitiation();
        }

        private void ReadTemperature()
        {
            double myTemp = 0;
            bool showZero = true;

///THIS IS CONVERT ON DEMAND OPTION
///            dsPowerBackConfig(0x90,0x03);
///            dsCommStartConvert(0x90);
///            dsCheckBusy(0x91);
///********************************
            ///THIS IS AUTO CONVERT

          //  dsPowerBackConfig(0x90, 0x02); //continious conversion (02) Command
            ///********************
// myTemp = dsCommReadTemperature(0x91, false);
            //digitalDisplayControl1.DigitText = DateTime.Now.ToString("HH:mm:ss");
            showZero = true;

            if (T_Present[0] && checkBoxActive90.Checked)
            {
                myTemp = dsCommReadTemperature(T_R_Addr[0], false);
                digitalDisplayControl90.DigitText = string.Format("{0:0.00}", myTemp);
                if (radioDefault90.Checked)
                {
                    showZero = false;
                    analogValueDraw(myTemp);
                }
                Thread.Sleep(250);
            }
            else
            {
                digitalDisplayControl90.DigitText = "88.88";
            }
            if (T_Present[1] && checkBoxActive92.Checked)
            {
                myTemp = dsCommReadTemperature(T_R_Addr[1], false);
                digitalDisplayControl92.DigitText = string.Format("{0:0.00}", myTemp);

                if (radioDefault92.Checked)
                {
                    showZero = false;
                    analogValueDraw(myTemp);
                }
                Thread.Sleep(250);
            }
            else
            {
                digitalDisplayControl92.DigitText = "88.88";
            }
            if (T_Present[2] && checkBoxActive94.Checked)
            {
                myTemp = dsCommReadTemperature(T_R_Addr[2], false);
                digitalDisplayControl94.DigitText = string.Format("{0:0.00}", myTemp);
                if (radioDefault94.Checked)
                {
                    showZero = false;
                    analogValueDraw(myTemp);
                }
                Thread.Sleep(250);
            }
            else
            {
                digitalDisplayControl94.DigitText = "88.88";
            }

            if (T_Present[3] && checkBoxActive96.Checked)
            {
                myTemp = dsCommReadTemperature(T_R_Addr[3], false);
                digitalDisplayControl96.DigitText = string.Format("{0:0.00}", myTemp);
                if (radioDefault96.Checked)
                {
                    showZero = false;
                    analogValueDraw(myTemp);
                }
                Thread.Sleep(250);
            }
            else
            {
                digitalDisplayControl96.DigitText = "88.88";
            }
            if (T_Present[4] && checkBoxActive98.Checked)
            {
                myTemp = dsCommReadTemperature(T_R_Addr[4], false);
                digitalDisplayControl98.DigitText = string.Format("{0:0.00}", myTemp);
                if (radioDefault98.Checked)
                {
                    showZero = false;
                    analogValueDraw(myTemp);
                }
                Thread.Sleep(250);
            }
            else
            {
                digitalDisplayControl98.DigitText = "88.88";
            }
            if (T_Present[5] && checkBoxActive9A.Checked)
            {
                myTemp = dsCommReadTemperature(T_R_Addr[5], false);
                digitalDisplayControl9A.DigitText = string.Format("{0:0.00}", myTemp);
                if (radioDefault9A.Checked)
                {
                    showZero = false;
                    analogValueDraw(myTemp);
                }
                Thread.Sleep(250);
            }
            else 
            {
                digitalDisplayControl9A.DigitText = "88.88";
            }
            if (T_Present[6] && checkBoxActive9C.Checked)
            {
                myTemp = dsCommReadTemperature(T_R_Addr[6], false);
                digitalDisplayControl9C.DigitText = string.Format("{0:0.00}", myTemp);
                if (radioDefault9C.Checked)
                {
                    showZero = false;
                    analogValueDraw(myTemp);
                }
                Thread.Sleep(250);
            }
            else
            {
                digitalDisplayControl9C.DigitText = "88.88";
            }
            if (T_Present[7] && checkBoxActive9E.Checked)
            {
                myTemp = dsCommReadTemperature(T_R_Addr[7], false);
                digitalDisplayControl9E.DigitText = string.Format("{0:0.00}", myTemp);
                if (radioDefault9E.Checked)
                {
                    showZero = false;
                    analogValueDraw(myTemp);
                }
                Thread.Sleep(250);
            }
            else
            {
                digitalDisplayControl9E.DigitText = "88.88";
            }

            if (showZero)
            {
                analogValueDraw(0);
            }
//Temperature.Text = String.Format("{0:0.0}", myTemp);
        }

        private void ReadTemp_Click(object sender, EventArgs e)
        {
            
     //       do
      //      {
              //  ReadTemp.Enabled = false;

     //           ReadTemperature();
           //     ReadTemp.Enabled = true;
              //  notifyIcon1.ShowBalloonTip(10, "Cloud's Title", "This is the message info", System.Windows.Forms.ToolTipIcon.Warning); 
            //    System.Threading.Thread.Sleep(100);
       //     } while (checkBoxAuto.Checked == true);
        }

        private void EE24C32_Resize(object sender, System.EventArgs e)
        {
            if (System.Windows.Forms.FormWindowState.Minimized == WindowState)
                Hide();
        }

        private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void analogMeter1_Click(object sender, EventArgs e)
        {
            Hide();
        }



        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int progress = 0;
            sensorsInitiation();
            for (int i = 1; i <= 100000; i++) {

                 if (backgroundWorker1.CancellationPending)
                     {
                    e.Cancel = true;
                     } else
                {
                    ReadTemperature();

                    backgroundWorker1.ReportProgress(progress);
                    if (progress == 100)
                    { 
                        progress = 0;
                    }
                    else
                    {
                        progress++;
                    }
                }
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            refreshUpdateDisplay();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar1.Value = 0;
            btnRunAsync.Enabled = true;
            Ports.Enabled = true;
            buttonSensorsDetect.Enabled = true;
        }

        private void btnRunAsync_Click(object sender, EventArgs e)
        {
            btnRunAsync.Enabled = false;
            Ports.Enabled = false;
            buttonSensorsDetect.Enabled = false;
            backgroundWorker1.RunWorkerAsync();
        }

        private void btnCancelAsync_Click(object sender, EventArgs e)
        {
            btnRunAsync.Enabled = true;
            Ports.Enabled = true;
            buttonSensorsDetect.Enabled = true;
            analogValueDraw(0);
            backgroundWorker1.CancelAsync();
        }



        private void backgroundScanSensors_DoWork(object sender, DoWorkEventArgs e)
        {
            //int progress = 0;

            for (int i = 0; i <= 7; i++)
            {

                if (backgroundScanSensors.CancellationPending)
                {
                    e.Cancel = true;
                }
                else
                {

                    double myTemp = 0;
                    myTemp = dsCommReadTemperature(T_R_Addr[i], false);
                    if (myTemp == 255.5)
                    {
                        T_Present[i] = false;
                    }
                    else
                    {
                        T_Present[i] = true;
                    }
              //      backgroundScanSensors.ReportProgress(i);

                    myTemp = 0.0;
                    // main detect instruction
                    Thread.Sleep(250);
                 //   
                  //  i++;
                   // progress = progress + 12;
                 //   backgroundScanSensors.ReportProgress(2);
                   //     ReportProgress(i);
               //     if (progress == 100)
                //    {
                 ///       progress = 0;
                 //   }
                 //   else
                 //   {
                 //       progress++;
                  //  }
                }
            }
        }

        private void backgroundScanSensors_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
         //   checkConfigFieldsAvailable(e.);
            progressScanBar.Value = e.ProgressPercentage;
        }

        private void backgroundScanSensors_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressScanBar.Value = 0;
         //   btnRunAsync.Enabled = true;
            Ports.Enabled = true;
            buttonSensorsDetect.Enabled = true;
            buttonSensorsDetectStop.Enabled = false;
            for (int i = 0; i <= 7; i++)
            {
                checkConfigFieldsAvailable(i);
            }
            refreshUpdateDisplay();
        }

        private void buttonSensorsDetect_Click(object sender, EventArgs e)
        {
            buttonSensorsDetect.Enabled = false;
            buttonSensorsDetectStop.Enabled = true;
            Ports.Enabled = false;
           // Thread.Sleep(4000);
          //  buttonSensorsDetect.Enabled = false;
            backgroundScanSensors.RunWorkerAsync();
        }

        private void buttonSensorsDetectStop_Click(object sender, EventArgs e)
        {
            buttonSensorsDetect.Enabled = true;
            buttonSensorsDetectStop.Enabled = false;
       //     btnRunAsync.Enabled = true;
            Ports.Enabled = true;
         //   buttonSensorsDetect.Enabled = true;
        //    analogValueDraw(0);
            for (int i = 0; i <= 7; i++)
            {
                checkConfigFieldsAvailable(i);
            }
            refreshUpdateDisplay();
            backgroundScanSensors.CancelAsync();
              //  .CancelAsync();
        }



        private void checkBoxActiveFront90_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActiveFront90.Checked)
            {
                checkBoxActive90.Checked = true;
            }
            else
            {
                checkBoxActive90.Checked = false;
            }
        }

        private void checkBoxActive90_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActive90.Checked)
            {
                checkBoxActiveFront90.Checked = true;
            }
            else
            {
                checkBoxActiveFront90.Checked = false;
            }
        }

        private void checkBoxActiveFront92_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActiveFront92.Checked)
            {
                checkBoxActive92.Checked = true;
            }
            else
            {
                checkBoxActive92.Checked = false;
            }
        }

        private void checkBoxActive92_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActive92.Checked)
            {
                checkBoxActiveFront92.Checked = true;
            }
            else
            {
                checkBoxActiveFront92.Checked = false;
            }
        }

        private void checkBoxActiveFront94_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActiveFront94.Checked)
            {
                checkBoxActive94.Checked = true;
            }
            else
            {
                checkBoxActive94.Checked = false;
            }
        }

        private void checkBoxActive94_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActive94.Checked)
            {
                checkBoxActiveFront94.Checked = true;
            }
            else
            {
                checkBoxActiveFront94.Checked = false;
            }
        }

        private void checkBoxActiveFront96_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActiveFront96.Checked)
            {
                checkBoxActive96.Checked = true;
            }
            else
            {
                checkBoxActive96.Checked = false;
            }
        }

        private void checkBoxActive96_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActive96.Checked)
            {
                checkBoxActiveFront96.Checked = true;
            }
            else
            {
                checkBoxActiveFront96.Checked = false;
            }

        }

        private void checkBoxActiveFront98_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActiveFront98.Checked)
            {
                checkBoxActive98.Checked = true;
            }
            else
            {
                checkBoxActive98.Checked = false;
            }
        }

        private void checkBoxActive98_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActive98.Checked)
            {
                checkBoxActiveFront98.Checked = true;
            }
            else
            {
                checkBoxActiveFront98.Checked = false;
            }
        }

        private void checkBoxActiveFront9A_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActiveFront9A.Checked)
            {
                checkBoxActive9A.Checked = true;
            }
            else
            {
                checkBoxActive9A.Checked = false;
            }
        }

        private void checkBoxActive9A_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActive9A.Checked)
            {
                checkBoxActiveFront9A.Checked = true;
            }
            else
            {
                checkBoxActiveFront9A.Checked = false;
            }
        }

        private void checkBoxActiveFront9C_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActiveFront9C.Checked)
            {
                checkBoxActive9C.Checked = true;
            }
            else
            {
                checkBoxActive9C.Checked = false;
            }
        }

        private void checkBoxActive9C_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActive9C.Checked)
            {
                checkBoxActiveFront9C.Checked = true;
            }
            else
            {
                checkBoxActiveFront9C.Checked = false;
            }
        }

        private void checkBoxActiveFront9E_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActiveFront9E.Checked)
            {
                checkBoxActive9E.Checked = true;
            }
            else
            {
                checkBoxActive9E.Checked = false;
            }
        }

        private void checkBoxActive9E_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActive9E.Checked)
            {
                checkBoxActiveFront9E.Checked = true;
            }
            else
            {
                checkBoxActiveFront9E.Checked = false;
            }
        }


    }
}
